package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * 思路:
     *  user数据:User(id=null, username=abc, password=123, phone=null, email=null, status=null)
     *  1.将密码明文,转化为密文   sha1/md5算法/md5hash
     *  2.根据用户名和密码查询用户信息
     *  3.有结果:   返回token  UUID
     *  4.没有结果: 返回null
     * @param user
     * @return
     */


    /**
     * 分页查询的Sql:
     * SELECT * FROM USER LIMIT 起始位置,查询条数
     * 第一页: 每页10条
     * SELECT * FROM USER LIMIT 0,10
     * 第二页: 每页10条
     * SELECT * FROM USER LIMIT 10,10
     * 第N页:
     * SELECT * FROM USER LIMIT (n-1)*10,10
     *
     * @param pageResult
     * @return
     */
    @Override
    public PageResult findUserList(PageResult pageResult) {
        //1.总记录数
        long total = userMapper.findTotal();
        //2.获取分页查询的结果
        int start = (pageResult.getPageNum() - 1) * pageResult.getPageSize();
        int size = pageResult.getPageSize();
        String query = pageResult.getQuery();
        List<User> rows = userMapper.findUserList(start, size, query);
        return pageResult.setTotal(total).setRows(rows);
    }

    /**
     * 需求: 修改状态信息
     * 参数: id/status/updated修改时间
     *
     * @param user
     */
    @Override
    @Transactional
    public void updateStatus(User user) {
        user.setUpdated(new Date()); //设定当前时间
        userMapper.updateStatus(user);
    }

    @Override
    @Transactional
    public void deleteUserById(Integer id) {

        userMapper.deleteUserById(id);
    }

    /**
     * 业务:
     * 1.密码进行加密处理
     * 2.设定状态码信息
     * 3.设定创建时间和修改时间
     *
     * @param user
     */
    @Override
    //@Transactional(rollbackFor = Exception.class) //事务的注解
    @Transactional
    public void saveUser(User user) {
        //将密码加密处理
        String password = user.getPassword();
        Date date = new Date();
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5Pass)
                .setStatus(true)
                .setCreated(date)
                .setUpdated(date);
        userMapper.saveUser(user);
    }

    @Override
    public User findUserById(Integer id) {

        return userMapper.findUserById(id);
    }

    /**
     * 说明: 实现用户的修改
     * 数据: 添加更新时间操作
     *
     * @param user
     */
    @Override
    @Transactional
    public void updateUser(User user) {
        user.setUpdated(new Date());
        userMapper.updateUser(user);

    }

    @Override
    public String login(User user) {
        byte[] bytes = user.getPassword().getBytes();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5DigestAsHex);
        User userDB = userMapper.selectUP(user);
        if (userDB == null) {
            return null;
        }
        String token = UUID.randomUUID().toString();
        return token;
    }
}
